package com.xywy.bloodandroid.act;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;

import org.apache.http.util.EncodingUtils;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.NumberPicker;
import android.widget.NumberPicker.Formatter;
import android.widget.NumberPicker.OnScrollListener;
import android.widget.NumberPicker.OnValueChangeListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.xywy.bloodandroid.R;
import com.xywy.bloodandroid.base.BaseActivity;
import com.xywy.bloodandroid.bean.User;
import com.xywy.bloodandroid.util.Constants;
import com.xywy.bloodandroid.util.UIUtil;
import com.xywy.bloodandroid.vollery.OnVolleyResponseListener;
import com.xywy.bloodandroid.vollery.PostRequest;
import com.xywy.bloodandroid.vollery.VolleyManager;
import com.xywy.bloodandroid.widget.MyDialog;

/**
 * *****************************************
 * 
 * @Title: PlusInfoTwoAct.java
 * @Package
 * @Description: TODO(加号基本信息2)
 * @author smz
 * @date 2015-1-12 下午6:18:39
 * @version V1.0
 ******************************************* 
 */
public class PlusInfoTwoAct extends BaseActivity implements OnScrollListener,
		Formatter {

	private LinearLayout ll_disease;
	private LinearLayout ll_symptoms;
	private RelativeLayout left_img;
	private TextView tv_disease;
	private TextView tv_symptoms;
	private TextView dialogTitle;
	private String[] disease = { "诊断", "治疗","复诊" };
	private TextView tv_goal;
	private LinearLayout ll_goal;
	private TextView back_tv;
	private String doctor_id;
	private String expert_id;
	private String uname;
	private String province;
	private String pnum;
	private String mphone;
	private TextView commit_tv;
	private String date;
	private String id;

	@Override
	protected void initView(Bundle savedInstanceState) {
		setContentView(R.layout.act_plus_two);
		left_img = (RelativeLayout) findViewById(R.id.left_img);
		TextView middle_title = (TextView) findViewById(R.id.middle_title);
		middle_title.setText("所患疾病");

		ll_disease = (LinearLayout) findViewById(R.id.ll_disease);
		tv_disease = (TextView) findViewById(R.id.tv_disease);
		ll_symptoms = (LinearLayout) findViewById(R.id.ll_symptoms);
		tv_symptoms = (TextView) findViewById(R.id.tv_symptoms);
		ll_goal = (LinearLayout) findViewById(R.id.ll_goal);
		tv_goal = (TextView) findViewById(R.id.tv_goal);
		back_tv = (TextView) findViewById(R.id.back_tv);
		commit_tv = (TextView) findViewById(R.id.commit_tv);
	}

	@Override
	protected void initListener() {
		ll_disease.setOnClickListener(this);
		ll_symptoms.setOnClickListener(this);
		left_img.setOnClickListener(this);
		ll_goal.setOnClickListener(this);
		back_tv.setOnClickListener(this);
		commit_tv.setOnClickListener(this);
	}

	@Override
	protected void initData() {

		doctor_id = getIntent().getStringExtra("doctor_id");
		expert_id = getIntent().getStringExtra("expert_id");
		date = getIntent().getStringExtra("date");
        id = getIntent().getStringExtra("id");
		uname = getIntent().getStringExtra("uname");
		province = getIntent().getStringExtra("province");

		pnum = getIntent().getStringExtra("pnum");
		mphone = getIntent().getStringExtra("mphone");
	   
	
	}
    /**
     * 完成加号信息
     */
	private void SubmitAddPlus() {
		String ill = "";
		String objective="";
		String lastresult="";
		try {
			uname=URLEncoder.encode(uname, "utf-8");
			province=URLEncoder.encode(province, "utf-8");
			ill=URLEncoder.encode(tv_disease.getText().toString().trim(), "utf-8");
			objective=URLEncoder.encode(tv_goal.getText().toString().trim(), "utf-8");
			lastresult=URLEncoder.encode(tv_symptoms.getText().toString().trim(), "utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		}
		PostRequest<String>  request=new PostRequest<String>(Constants.getUrl("zhuanjia","PlusSubmit")+"&doctor_id="+doctor_id, String.class, new OnVolleyResponseListener<String>() {

			@Override
			public void onError(VolleyError volleyError) {
				UIUtil.DissDialog(context, waittingDialog);
				UIUtil.showToast(context, context.getString(R.string.network_slow));
			}

			@Override
			public void onSuccess(String response) {
				UIUtil.DissDialog(context, waittingDialog);
				JSONObject object;
				try {
					object = new JSONObject(response);
					int code=object.optInt("code");
					String msg=object.optString("msg");
					if(code==10000){
						UIUtil.showToast(context, "预约成功");
						enterActivity(MainFragmentAct.class);
					}else{
						UIUtil.showToast(context, msg);
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
				
			}
		});
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("doctor_id",doctor_id);
		params.put("c_uid",User.getInstance(context).getUserid());//
		params.put("uname", uname);
		params.put("expert_id", expert_id);
		params.put("date", date);
		params.put("ill", ill);//tv_disease.getText().toString().trim()
		params.put("pnum",pnum );
		params.put("objective",objective);//tv_goal.getText().toString().trim()
		params.put("lastresult", lastresult);//tv_symptoms.getText().toString().trim()
		params.put("province", "");//
		params.put("city", "");
		params.put("is_client","1");
		params.put("is_new_user","0");
		params.put("sex","女");
		params.put("img","");
		params.put("birthday","1985-09-18");
		params.put("pdid", id);
		params.put("is_client","1");
		params.put("mphone",mphone);
		
		request.setParams(params);
		request.setIsParseJson(false);
		executeRequest(request);
//		VolleyManager.addRequest(request, this);
 	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.left_img:
			finish();
			break;
		case R.id.ll_disease:
			Intent intent1 = new Intent(context, TheIllnessAct.class);
			startActivityForResult(intent1, 1);
			break;
		case R.id.ll_symptoms:
			Intent intent2 = new Intent(context, SymptomsAct.class);
			startActivityForResult(intent2, 2);
			break;
		case R.id.ll_goal:
			showDiseaseDialog();
			break;
		case R.id.back_tv:
			finish();
			break;
		case R.id.commit_tv:
			if(checkInfo())
			SubmitAddPlus();
			
			break;
		default:
			break;
		}
	}

	private boolean checkInfo() {
		String diserase=tv_disease.getText().toString().trim();
		String symptoms=tv_symptoms.getText().toString().trim();
		String goal=tv_goal.getText().toString().trim();
		if(diserase.length()<1||"请填写".equals(diserase)){
			UIUtil.showToast(context, "未选择所患疾病");
			return false;
		}
		if(symptoms.length()<1||"请填写".equals(symptoms)){
			UIUtil.showToast(context, "未选择您的症状表现");
			return false;
		}
		if(goal.length()<1){
			UIUtil.showToast(context, "未选择你的就诊目的");
			return false;
		}
		return true;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (data != null) {
			switch (requestCode) {
			case 1:
				String value1;
				value1 = data.getStringExtra("button_text");
				if (value1.length() > 0) {
					tv_disease.setText(value1);
				}
				break;
			case 2:
				String value2;
				value2 = data.getStringExtra("button_text");
				if (value2.length() > 0) {
					tv_symptoms.setText(value2);
				}

				break;
			}
		}

	}

	private void showDiseaseDialog() {
		final MyDialog myDialog = new MyDialog(PlusInfoTwoAct.this,
				R.style.MyDialogStyle);
		myDialog.show();
		myDialog.setContentView(R.layout.mydialog_twobutton_layout);
		dialogTitle = (TextView) myDialog.findViewById(R.id.mydialog_title);
		dialogTitle.setText("就诊目的 ");
		NumberPicker numberPicker = (NumberPicker) myDialog
				.findViewById(R.id.picker);
		numberPicker.setFormatter(this);
		numberPicker.setOnValueChangedListener(new OnValueChangeListener() {

			@Override
			public void onValueChange(NumberPicker picker, int oldVal,
					int newVal) {
				tv_goal.setText(disease[newVal]);
				// sp.edit().putString("baby_sex", sex[newVal]);
			}
		});
		numberPicker.setOnScrollListener(this);
		numberPicker.setDisplayedValues(disease);
		numberPicker.setMinValue(0);
		numberPicker.setMaxValue(disease.length - 1);
		if ("诊断".equals(tv_goal.getText().toString())) {
			numberPicker.setValue(0);
		} else if ("治疗".equals(tv_goal.getText().toString())) {
			numberPicker.setValue(1);
		}else if("复诊".equals(tv_goal.getText().toString())){
			numberPicker.setValue(2);
		}
		((EditText) numberPicker.getChildAt(0))
				.setOnFocusChangeListener(new OnFocusChangeListener() {

					@Override
					public void onFocusChange(View arg0, boolean arg1) {
						// TODO Nothing

					}
				});
		((EditText) numberPicker.getChildAt(0))
				.setInputType(InputType.TYPE_NULL);
		TextView leftButton = (TextView) myDialog
				.findViewById(R.id.mydialog_twobutton_layout_left_button);
		leftButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				myDialog.dismiss();
			}
		});
		myDialog.findViewById(R.id.mydialog_twobutton_layout_right_button)
				.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						myDialog.dismiss();

					}
				});

	}

	@Override
	public void onScrollStateChange(NumberPicker view, int scrollState) {
		switch (scrollState) {
		case OnScrollListener.SCROLL_STATE_FLING:

			break;
		case OnScrollListener.SCROLL_STATE_IDLE:

			break;
		case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:

			break;
		}
	}

	@Override
	public String format(int value) {
		return null;
	}
}
